// API 服务 - 使用后端接口
import {
	request,
	upload
} from '@/utils/request.js';

// 鉴权
export async function apiLogin(payload) {
	const {
		username,
		password
	} = payload || {};
	if (!username || !password) {
		throw new Error('用户名和密码不能为空');
	}
	return request({
		url: '/auth/login',
		method: 'POST',
		data: {
			username,
			password
		}
	});
}
export async function apiGetUserProjects() {
	return request({
		url: '/problem_reports/projects',
		method: 'GET'
	});
}

// 报告
export async function apiCreateReport({
	userId,
	projectId,
	address,
	locationDetail,
	description,
	images = []
}) {
	return request({
		url: '/problem_reports',
		method: 'POST',
		data: {
			userId,
			projectId,
			address,
			locationDetail,
			description,
			images
		}
	});
}
export async function apiUploadReportImage(filePath) {
	return upload({
		url: '/upload/image',
		filePath,
		name: 'file',
		formData: {
			'type': 'image', // 明确指定类型
		}
	});
}
export async function apiGetMyReportHistory(id) {
	return request({
		url: `/list?userId=${id}`,
		method: 'GET'
	});
}
export async function apiGetReportDetail(id) {
	return request({
		url: `/list/${id}`,
		method: 'GET'
	});
}